package com.indusfo.spc.pda.service.impl;

import com.indusfo.spc.pda.mapper.CallmatDosingDetailMapper;
import com.indusfo.spc.pda.mapper.CallmatDosingMapper;
import com.indusfo.spc.pda.pojo.CallmatDosing;
import com.indusfo.spc.pda.pojo.CallmatDosingDetail;
import com.indusfo.spc.pda.pojo.CallmatDto;
import com.indusfo.spc.pda.service.CallmatDosingDetailService;
import com.indusfo.spc.pojo.Mat;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @ClassName CallmatDosingDetailImpl
 * @Description 超市叫料明细service
 * @Author louk
 * @Date 2019/12/20 15:38
 */
@Service
public class CallmatDosingDetailServiceImpl implements CallmatDosingDetailService {

    private static final Logger logger= LoggerFactory.getLogger(CallmatDosingDetailService.class);
    @Resource
    CallmatDosingDetailMapper callmatDosingDetailMapper;
    @Resource
    CallmatDosingMapper callmatDosingMapper;


    /**
     * 新增超市叫料明细
     * @param callmatDto
     * @return
     */
    @Override
    @Transactional
    public JSONObject insertCallmatDosingDetail(CallmatDto callmatDto) {

        String checkResult = checkParam(callmatDto);
        if(!StringUtils.isEmpty(checkResult)){
            return JSONObject.build(JSONObject.ERROR,checkResult);
        }
        //物料id+叫料数量来查看扫码是否正确
        CallmatDosing callmatDosing=new CallmatDosing();
        callmatDosing.setMatId(callmatDto.getMatId());
        CallmatDosing matCallmatDosing = callmatDosingMapper.queryCallmatDosing(callmatDosing);
        if(matCallmatDosing==null){
            return JSONObject.build(JSONObject.ERROR,"未获取到配料信息");
        }
        //未配料数量(三种情况1:配送了一部分2:配送完成3:未配送)
        Integer noDosingNum = matCallmatDosing.getNoDosingNum();
        //未获取到相关配送数量则说明已经配送完成无需配送
        if(matCallmatDosing.getCallmatDosingId()==null || "".equals(matCallmatDosing.getCallmatDosingId())){
            return JSONObject.build(JSONObject.ERROR,"当前叫料配送完成无需进行配送");
        }
        if(noDosingNum!=null && !"".equals(noDosingNum) &&  noDosingNum<=0){
            return JSONObject.build(JSONObject.ERROR,"当前叫料配送完成无需进行配送");
        }
        if((noDosingNum==null || "".equals(noDosingNum)) && Integer.parseInt(matCallmatDosing.getDemandNum())< Integer.parseInt(callmatDto.getDosingNum())){
            return JSONObject.build(JSONObject.ERROR,"配送数量大于需求数量");
        }
        //执行新增
        CallmatDosingDetail callmatDosingDetail=new CallmatDosingDetail();
        callmatDosingDetail.setCallmatDosingId(matCallmatDosing.getCallmatDosingId());
        callmatDosingDetail.setDosingNum(callmatDto.getDosingNum());
        int insert = callmatDosingDetailMapper.insert(callmatDosingDetail);
        if(insert>0){
            //对超市叫料配送状态进行更新
            callmatDosingMapper.updateDosingState(matCallmatDosing.getCallmatDosingId());
            return JSONObject.oK("新增成功");
        }
//        callmatDosingDetailMapper.insert(callmatDosing);
        return JSONObject.build(JSONObject.ERROR,"新增失败");
    }


    /**
     * 根据物料批号来获取到物料编号,物料名称
     * @param matNo
     * @return
     */
    @Override
    public JSONObject queryDosingCallmat(String matNo) {
        CallmatDto callmatDosingDto=new CallmatDto();
        Map<String, Mat> matMap = callmatDosingMapper.queryMat();
        for (Map.Entry<String, Mat> matEntry : matMap.entrySet()) {
            if(matNo.contains(matEntry.getKey())){
                callmatDosingDto.setMatCode(matEntry.getKey());
                callmatDosingDto.setMatName(matEntry.getValue().getMatName());
                callmatDosingDto.setMatId(matEntry.getValue().getMatId());
                break;
            }
        }
        if(StringUtils.isEmpty(callmatDosingDto.getMatCode())){
            return JSONObject.build(JSONObject.ERROR,"暂无数据",null);
        }
        int callmatDosingCount = callmatDosingMapper.queryByMatId(callmatDosingDto.getMatId());
        if(callmatDosingCount<=0){
            return JSONObject.build(JSONObject.ERROR,"暂无相关叫料信息",null);
        }
        return JSONObject.build(JSONObject.SUCCESS,"查询成功",callmatDosingDto);
    }

    public String checkParam(CallmatDto callmatDto){
       Integer matId = callmatDto.getMatId();
       if(matId==null || "".equals(matId)){
           return "未获取到物料id";
       }
       String dosingNum = callmatDto.getDosingNum();
       if(StringUtils.isBlank(dosingNum)){
           return "未获取到叫料数量";
       }else {
           if(StringUtils.isNumeric(dosingNum)){
               if(Integer.parseInt(dosingNum)==0 || "".equals(Integer.parseInt(dosingNum))){
                        return "叫料数量必须为整数并且不能为0";
               }
           }else{
                   return "叫料数量必须为整数";
           }
       }
       return null;
   }
}
